﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

using CRM.Entity;
using CRM.BLL;
using DP.Web.UI;
using DP.Common;

namespace CRM.Web.SurveyManage
{
    public partial class SurveyItemsOptionsModify : Common.PageBase
    {
        T_SURVEY_BLL sBll = new T_SURVEY_BLL();
        T_SURVEY_ITEM_BLL iBll = new T_SURVEY_ITEM_BLL();
        T_SURVEY_ITEM_OPTION_BLL siopBll = new T_SURVEY_ITEM_OPTION_BLL();
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            PageHelper.SetPrePageUrl(this.Page);
            PageHelper.SetGoBackUrl(this.Page, btnCancel, StaticConstant.NotBackUrl);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //ddlNEXT_ITEMBinding();
                pageDataInit();
            }
        }
        #region 数据绑定
        private void ddlNEXT_ITEMBinding()
        {
            string _autoid = String.IsNullOrEmpty(Request.QueryString["AUTOID"]) ? "" : Request.QueryString["AUTOID"].Trim();
            T_SURVEY_ITEM_OPTION_Entity siopEntity = new T_SURVEY_ITEM_OPTION_Entity();
            siopEntity = siopBll.GetEntity("AUTOID", StringHelper.ToInt(_autoid));
            ddlNEXT_ITEM.DataSource = iBll.GetList(String.Format(" AND SURVEY_ID='{0}' AND ITEM_ID<>'{1}' ", siopEntity.SURVEY_ID, siopEntity.ITEM_ID));
            ddlNEXT_ITEM.DataTextField = "ITEM_NAME";
            ddlNEXT_ITEM.DataValueField = "ITEM_ID";
            ddlNEXT_ITEM.DataBind();
            ddlNEXT_ITEM.Items.Insert(0, new ListItem("", ""));
        }
        #endregion
        private void pageDataInit()
        {
            string _autoid  = String.IsNullOrEmpty(Request.QueryString["AUTOID"]) ? "" : Request.QueryString["AUTOID"].Trim();
            string _surveyid = String.IsNullOrEmpty(Request.QueryString["SURVEY_ID"]) ? "" : Request.QueryString["SURVEY_ID"].Trim();
            string _itemid = String.IsNullOrEmpty(Request.QueryString["ITEM_ID"]) ? "" : Request.QueryString["ITEM_ID"].Trim();
            if (!String.IsNullOrEmpty(_autoid))
            {
                T_SURVEY_ITEM_OPTION_Entity siopEntity = new T_SURVEY_ITEM_OPTION_Entity();
                siopEntity = siopBll.GetEntity("AUTOID", StringHelper.ToInt(_autoid));
                ddlNEXT_ITEM.SelectedValue = siopEntity.NEXT_ITEM.ToString();
                PageHelper.SetControlValue(this.Page, siopEntity);
                lblTitle.Text = "选项信息修改";
            }
            else
            {
                T_SURVEY_Entity entity = sBll.GetEntity("SURVEY_ID", _surveyid);
                if (entity != null)
                {
                    txtSURVEY_ID.Text = entity.SURVEY_ID.ToString();
                    txtSURVEY_NAME.Text = entity.SNAME;
                }
                T_SURVEY_ITEM_Entity iEntity = iBll.GetList(String.Format(" AND SURVEY_ID='{0}' AND ITEM_ID='{1}'",_surveyid,_itemid)).FirstOrDefault();
                if (iEntity != null)
                {
                    txtITEM_ID.Text=iEntity.ITEM_ID.ToString();
                    txtITEM_NAME.Text = iEntity.ITEM_NAME;
                }
                string optionid = siopBll.GetMaxValue("OPTION_ID", String.Format(" AND SURVEY_ID='{0}' AND ITEM_ID='{1}'", txtSURVEY_ID.Text, txtITEM_ID.Text));
                int i = 1;
                if (!String.IsNullOrEmpty(optionid))
                {
                    i = StringHelper.ToInt(optionid) + 1;
                }
                txtOPTION_ID.Text = i.ToString();
                txtOPTION_NAME.Text = "";
                ddlNEXT_ITEM.Text = "";
                lblTitle.Text = "选项信息添加";
            }
        }
        /// <summary>
        /// 保存数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnOK_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(txtOPTION_NAME.Text))
            {
                PageHelper.ShowMessage(this.Page, "选项名称不能为空！");
                return;
            }
            if (ddlNEXT_ITEM.SelectedValue == txtITEM_ID.Text)
            {
                PageHelper.ShowMessage(this.Page, "当前题目的下一题不能为自己！");
                return;
            }
            T_SURVEY_ITEM_OPTION_Entity siopEntity = new T_SURVEY_ITEM_OPTION_Entity();
            siopEntity = siopBll.GetList(String.Format(" AND SURVEY_ID='{0}' and ITEM_ID='{1}' and (OPTION_ID='{2}' or OPTION_NAME='{2}')", txtSURVEY_ID.Text, txtITEM_ID.Text, txtOPTION_ID.Text)).FirstOrDefault();
            if (siopEntity == null)
            {
                siopEntity = new T_SURVEY_ITEM_OPTION_Entity();
                siopEntity = siopBll.GetList(String.Format(" AND SURVEY_ID='{0}' and ITEM_ID='{1}' and OPTION_NAME='{2}'", txtSURVEY_ID.Text, txtITEM_ID.Text, txtOPTION_NAME.Text)).FirstOrDefault();
                if (siopEntity == null)
                {
                    siopEntity = new T_SURVEY_ITEM_OPTION_Entity();
                    PageHelper.GetControlValue(this.Page, ref siopEntity);
                    siopEntity.NEXT_ITEM = StringHelper.ToInt(ddlNEXT_ITEM.SelectedValue);
                    siopEntity.BUSINESSDIVISIONCODE = _userLoginInfo.UserEntity.BUSINESSDIVISIONCODE;
                    siopEntity.BUSINESSDIVISIONID = _userLoginInfo.UserEntity.BUSINESSDIVISIONID;
                    siopEntity.BUSINESSDIVISIONNAME = _userLoginInfo.UserEntity.BUSINESSDIVISIONNAME;
                    siopBll.Insert(siopEntity);
                }
                else
                {
                    PageHelper.ShowMessage(this.Page, String.Format("当前题目选项名称{0}已经存在，请核实后再进行增加！", txtOPTION_NAME.Text));
                    return;
                }
            }
            else
            {
                PageHelper.GetControlValue(this.Page, ref siopEntity);
                siopEntity.NEXT_ITEM = StringHelper.ToInt(ddlNEXT_ITEM.SelectedValue);
                siopBll.Update(siopEntity);
            }
            PageHelper.ShowMessage(this.Page, "保存成功！");
            Response.Redirect(String.Format("SurveyItemsOptionsList.aspx?Survey_ID={0}&Survey_NAME={1}&ITEM_ID={2}&ITEM_NAME={3}", txtSURVEY_ID.Text, txtSURVEY_NAME.Text,txtITEM_ID.Text,txtITEM_NAME.Text));
        }
    }
}
